<?php
/**
 * Created by PhpStorm.
 * User: yangwang
 * Date: 2024-04-18
 * Time: 15:21
 */

namespace App\Console\Commands\Shop;


use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\Shop\Model\ShopStaffRecordLogModel;
use App\Kuafu\Shop\Model\ShopStaffRecordModel;
use App\Kuafu\Shop\Service\ShopStaffRecordLogService;
use App\Kuafu\User\Model\User;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Log;

class CheckHealthCard extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'shop:check_health_card';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '检查健康证是否过期';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public static $expireUserIds = [];

    public function handle()
    {
        Log::info("更新健康证状态开始运行");

        $expireUserIds = $aboutExpireUserIds = [];
        User::query()
            ->where('healthCardEndDate', '>', strtotime('-2 day'))
            ->where('healthCardEndDate', '<', strtotime('+31 day'))
            ->chunk(100, function (Collection $users) use (&$expireUserIds, &$aboutExpireUserIds) {
                foreach ($users as $user) {
                    $healthCardEndDate = $user->healthCardEndDate;
                    if (ShopStaffRecordModel::healthCardStatus(
                            $healthCardEndDate
                        ) == ShopStaffRecordModel::HEALTH_CARD_STATUS_EXPIRED) {
                        $expireUserIds[$user->id] = $user->id;
                    }
                    if (ShopStaffRecordModel::healthCardStatus(
                            $healthCardEndDate
                        ) == ShopStaffRecordModel::HEALTH_CARD_STATUS_ABOUT_TO_EXPIRE) {
                        $aboutExpireUserIds[$user->id] = $user->id;
                    }
                }
            });

        $userIds = array_merge(array_values($expireUserIds), array_values($aboutExpireUserIds));
        foreach (array_chunk($userIds, 100) as $userIdChunk) {
            Artisan::call('shop:refresh_shop_staff_statistics --userIds=' . join(',', $userIdChunk));
        }

        foreach (array_chunk(array_values($expireUserIds), 100) as $chunkExpireUserIds) {
            ShopStaffRecordModel::query()
                                ->whereIn('userId', $chunkExpireUserIds)
                                ->chunk(100, function (Collection $shopStaffRecordList) {
                                    $insertData = [];
                                    /** @var ShopStaffRecordModel $shopStaffRecord */
                                    foreach ($shopStaffRecordList as $shopStaffRecord) {
                                        $insertData[] = [
                                            'shopId'       => $shopStaffRecord->shopId,
                                            'shopCode'     => $shopStaffRecord->shopCode,
                                            'userId'       => $shopStaffRecord->userId,
                                            'operateType'  => ShopStaffRecordLogModel::OPERATE_TYPE_SYS,
                                            'actionType'   => ShopStaffRecordLogModel::ACTION_TYPE_CARD_EXPIRED,
                                            'remark'       => '',
                                            'createdByUid' => 0,
                                            'updatedByUid' => 0,
                                            'createdAt'    => time(),
                                            'updatedAt'    => time(),
                                        ];
                                        Log::info("更新健康证状态:user_id:{$shopStaffRecord->userId},shop_id:{$shopStaffRecord->shopId}");
                                    }

                                    if ($insertData) {
                                        ShopStaffRecordLogService::batchAddRecord($insertData);
                                    }
                                });
        }

        Log::info("更新健康证状态结束运行");
    }

}